# -*- encoding: utf-8 -*-
import json
import subprocess
from Common.mysql import app
from flask import jsonify, request
from src.User.token import token_verify, before_request, token_user_id
from Common.yaml_method import YamlMethod
from src.ApiTest.ApiCase.Function.add_api_case import AddApiCase
from src.ApiTest.ApiCase.Function.api_case_list import ApiCaseList
from src.ApiTest.ApiCase.Function.delete_api_case import DeleteApiCase
from src.ApiTest.ApiCase.Function.update_api_case import UpdateApiCase
from src.TestApiRun.run_case import RunCase

code = YamlMethod().read_data('code.yaml')['code']


@app.route('/api_case/add', methods=['POST'])
@token_verify
def add_api_case(create_user):
    """
    添加接口测试用例接口
    :param create_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    data_name = data['caseName']
    project_name = data['projectName'].split('/')[0]
    project_id = data['projectName'].split('/')[1]
    request_info = data['requestInfo']

    # 校验参数
    if not all([data_name, project_name, request_info, create_user]):
        return jsonify(code=code[7], msg="信息不完整")

    # 提交信息
    res = AddApiCase().add_api_case(data_name, project_id, project_name, request_info, create_user)
    return jsonify(res)


@app.route('/api_case/list', methods=['POST'])
@before_request
def api_case_list():
    """
    获取接口测试用例列表接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    page = data['page']
    limit = data['limit']
    data_name = data['caseName']
    project_name = data['projectName']

    res = ApiCaseList().api_case_list(page, limit, data_name, project_name)
    return res


@app.route('/api_case/delete', methods=['POST'])
@before_request
def delete_api_case():
    """
    删除接口测试用例接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    data_id = data['id']

    res = DeleteApiCase().delete_api_case(data_id)
    return res


@app.route('/api_case/update', methods=['POST'])
@token_verify
def update_api_case(update_user):
    """
    更新接口测试用例
    :param update_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))

    case_id = data['id']
    case_name = data['caseName']
    project_name = data['projectName']
    project_id = data['projectId']
    request_info = data['requestInfo']

    # 校验参数
    if not all([case_id, case_name, request_info, update_user]):
        return jsonify(code=code[7], msg="请求信息不完整")

    res = UpdateApiCase().update_api_case(case_id, case_name, project_id, project_name, request_info, update_user)
    return res


@app.route('/api_case/debug', methods=['POST'])
@token_user_id
def api_case_debug(user_id):
    """
    接口用例调试接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))

    case_name = data['caseName']
    project_id = data['projectId']
    request_info = data['requestInfo']
    environment = data['environment']

    # 校验参数
    if not all([project_id, request_info, environment]):
        return jsonify(code=code[7], msg="信息不完整")

    res_data = RunCase().run_case(case_name, project_id, request_info, environment, user_id)
    res = {
        'code': code[0],
        'data': str(res_data)
    }
    return res